//
//  BBRechargeViewController.m
//  XTXOneToOne
//
//  Created by Canny on 2018/11/5.
//  Copyright © 2018年 WK. All rights reserved.
//

#import "BBRechargeViewController.h"
#import "BBRechargeModel.h"

@interface BBRechargeViewController ()

@property (nonatomic,strong)UIImageView * codeImgV;
@property (nonatomic,strong)UILabel * code;
@property (nonatomic,strong)BBRechargeModel * BBmodel;

@end

@implementation BBRechargeViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setProperty];
   

}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarHidden = NO;
    
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBarHidden=YES;
    [self Http_address_finddeposit];
}

-(void)setProperty{
    
    self.view.backgroundColor=RGB(255, 255, 255);
    
    [UIApplication sharedApplication].statusBarHidden = NO;
    
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBarHidden=YES;
    
    self.baseVcTitle = [NSString stringWithFormat:@"%@充值",_dataSource.symbol];
    
    
}

-(void)createUI{
    
    _codeImgV = [[UIImageView alloc]init];
    [self.bgView addSubview:_codeImgV];

    [_codeImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView.mas_top).offset(20);
        make.centerX.equalTo(self.bgView.mas_centerX);
        make.width.equalTo(@(150));
        make.height.equalTo(@(150));
    }];
    UILabel * lab1 = [[UILabel alloc]init];
    [self.bgView addSubview:lab1];
    [self setLab:lab1 withColor:ThemeColor andFont:18 andText:@"扫描二维码" andTextAlignment:NSTextAlignmentCenter];
    [lab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.codeImgV.mas_bottom).offset(10);
        make.centerX.equalTo(self.bgView.mas_centerX);
        make.width.equalTo(@(250));
        make.height.equalTo(@(25));
    }];
    
    _code= [[UILabel alloc]init];
    [self.bgView addSubview:_code];
    [self setLab:_code withColor:ThemeColor andFont:15 andText:@"" andTextAlignment:NSTextAlignmentCenter];
    _code.numberOfLines = 0;
    [_code mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lab1.mas_bottom).offset(10);
        make.centerX.equalTo(self.bgView.mas_centerX);
        make.width.equalTo(@(250));
//        make.height.equalTo(@(25));
    }];
    
    UIButton *  btn_copy = [[UIButton alloc]init];
    [self.bgView addSubview:btn_copy];
    [btn_copy setTitleColor:ThemeColor forState:UIControlStateNormal];
    [btn_copy setTitle:@"复制地址" forState:UIControlStateNormal];
    btn_copy.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    btn_copy.titleLabel.font = [UIFont systemFontOfSize: 18.0];
    [btn_copy addTarget:self action:@selector(copyClick) forControlEvents:UIControlEventTouchUpInside];
    [btn_copy mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.code.mas_bottom).offset(50);
        make.centerX.equalTo(self.bgView.mas_centerX);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(30);
    }];
    
}

-(void)setLab:(UILabel *)lab withColor:(UIColor *)color andFont:(CGFloat)font andText:(NSString *)text andTextAlignment:(NSTextAlignment)textAlignment{
    lab.text=text;
    lab.textColor=color;
    lab.font=[UIFont systemFontOfSize:font];
    lab.textAlignment=textAlignment;
}

//获取用户充值地址
//
//1、接口地址
//
//请求方式：post
//请求接口：address/finddeposit
//是否登录：是
//2、接口请求参数
//
//参数名    参数类型    是否必须    含义    备注
//currency_id    整型    是    币种id

-(void)Http_address_finddeposit{
    NSString * URL = [NSString stringWithFormat:@"%@address/finddeposit",DEFAULTSEVERBASEURL];
    
    NSDictionary * Dic =@{
                          @"currency_id":_dataSource.currency_id
                          };
    [HUD showProgress:@"" inView:self.bgView];
    WS(weakSelf);
    [MOLoadHttpsData PostHttpDataWithUrlStr:URL Dic:Dic SuccessBlock:^(id  _Nonnull responseObject) {
        
        [HUD dismissInView:self.bgView];
        NSDictionary * dic = responseObject;
        
        NSString * code = [NSString stringWithFormat:@"%@",dic[@"code"]];
        NSString * msg = [NSString stringWithFormat:@"%@",dic[@"msg"]];
        
        if ([code isEqualToString:@"0"]) {
            
             [weakSelf createUI];
            
            NSDictionary * dicc = dic[@"data"];
            weakSelf.BBmodel = [BBRechargeModel mj_objectWithKeyValues:dicc];
            
            [weakSelf relayOutUI];
            
        }else{
            [HUD showErrorMessage:msg];
        }
        
    } FailureBlock:^(id  _Nonnull error) {
        
        [HUD dismissInView:self.bgView];

    }];
    
}

-(void)copyClick{
    
    UIPasteboard *pab = [UIPasteboard generalPasteboard];
    
    NSString *string = _code.text;
    
    [pab setString:string];
    
    if (pab == nil) {

        [HUD showErrorMessage:@"复制失败"];
    }else
    {
        [HUD showSuccessMessage:@"已复制"];
    }
}

-(void)relayOutUI{
    
    [self creatCIQRCodeImage];
    _code.text = [NSString stringWithFormat:@"%@",_BBmodel.address];
}

/**
 *  生成二维码
 */
- (void)creatCIQRCodeImage
{
    // 1.创建过滤器，这里的@"CIQRCodeGenerator"是固定的
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    
    // 2.恢复默认设置
    [filter setDefaults];
    
    // 3. 给过滤器添加数据
    NSString *dataString = _BBmodel.address;
    NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
    // 注意，这里的value必须是NSData类型
    [filter setValue:data forKeyPath:@"inputMessage"];
    
    // 4. 生成二维码
    CIImage *outputImage = [filter outputImage];
    
    // 5. 显示二维码
    _codeImgV.image = [UIImage creatNonInterpolatedUIImageFormCIImage:outputImage withSize:150];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
